# -*- coding: utf-8 -*-
from __future__ import unicode_literals

"""
Created on Thu Dec 15 13:11:54 2016

@author: aw
制造业 生存曲线
"""

import numpy as np
import pylab as pl


def shcfx(l_date, a, b):
    t = pl.frange(1, l_date, 1)  # x 轴
    y = (b / a * (t / a) ** (b - 1)) * np.exp(-(t / a) ** b)  # y轴
    yy = (b * (b - 1) / (a ** 2) * (t / a) ** (b - 2) - (b / a * (t / a) ** (b - 1)) ** 2) * np.exp(-(t / a) ** b)  # 点1
    l1 = 0
    l2 = np.argmin(yy)
    l3 = 0
    r = abs(np.min(y))
    for i in range(l_date - 1):
        if (yy[i] * yy[i + 1] < 0):
            i1 = abs(yy[i + 1] - yy[i])
            i2 = abs(yy[i + 1])
            z = i2 / i1
            l1 = t[i] * z + t[i + 1] * (1 - z)
            break
    distence = abs(l2 - l1) / 2.0
    for i in range(l_date - 1):
        if i > l2 + distence:
            if (abs(yy[i]) + abs(yy[i] - yy[i + 1])) <= r:
                l3 = i
                break
    if l3 < l2:
        l3 = l_date + 1
    else:
        l3 = t[l3]
    t = map(lambda x:x-1, t)
    return [t, y], [l1, t[l2], l3]

# if __name__ == '__main__':
#     a = 1.889
#     b = 8.571
#     print shcfx(a, b)
